package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import Classes.Cliente;
import Classes.Endereco;
import Nucleo.ConexaoBanco;

public class EnderecoDAO {

	private ConexaoBanco conn;
	
	public EnderecoDAO(){
		conn = new ConexaoBanco();
	}
	
	public void inserir( Endereco endereco , Cliente cliente){
		String sql = "INSERT INTO `Endereco` (`Cliente_idCliente`,`Rua`,`Numero`," +
				"`Complemento`,`Bairro`,`Cidade`,`Estado`) values ( ? , ? , ? , ? , ? , ? , ?) ";
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			stmt.setInt(1, cliente.getId() );
			stmt.setString(2, endereco.getRua() );
			stmt.setString(3, endereco.getNumero() );
			stmt.setString(4, endereco.getComplemento() );
			stmt.setString(5, endereco.getBairro() );
			stmt.setString(6, endereco.getCidade() );
			stmt.setString(7, endereco.getEstado() );
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public Endereco getEnderecoDoCliente( int idCliente ){
		String sql = "SELECT * FROM `Endereco` WHERE `Cliente_idCliente` = '"+ idCliente +"' ";
		Endereco endereco = null;
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			
			while ( rs.next() ) {
				endereco = new Endereco();
				
				endereco.setRua			( rs.getString("Rua") );
				endereco.setNumero		( rs.getString("Numero") );
				endereco.setComplemento	( rs.getString("Complemento") );
				endereco.setBairro		( rs.getString("Bairro") );
				endereco.setCidade		( rs.getString("Cidade") );
				endereco.setEstado		( rs.getString("Estado") );
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return endereco;
	}
	
	public boolean deletar( int idCliente ) {
		String sql = "DELETE FROM `endereco` WHERE `Cliente_idCliente` = '"+ idCliente+"'";
		
		try {
			Statement stmt = conn.getConnection().createStatement();
			int delete = stmt.executeUpdate(sql);
			if ( delete == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
		
		
	}

	
}
